<template>
  <div class="list wrapper" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-bottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <router-link to='/'>
            <div class="button">{{this.city}}</div>
            </router-link>
          </div>
        </div>
      </div>
      <div class="area">
      <div class="title border-topbottom">热门城市</div>
      <div class="button-list">
        <div class="button-wrapper" v-for="item of hot" :key="item.id" @click="handleCityClick(item.name)">
          <div class="button">{{item.name}}</div>
        </div>
      </div>
      </div>
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key"><!--ADCD区域 -->
        <div class="title border-topbottom">{{key}}</div>
          <ul class="item-list" v-for="innerItems of item" :key="innerItems.id"  @click="handleCityClick(innerItems.name)">
            <li class="item border-bottom">{{innerItems.name}}</li>
          </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { mapState,mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState(['city'])
  },
  mounted () {
        this.scroll = new BScroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if(this.letter){
        const element = this.$refs[this.letter][0]
        // console.log(element)
        this.scroll.scrollToElement(element,1000)
      }
    }
  },
  methods: {
    handleCityClick (city) {
      // this.$store.dispatch('MchangeCity', city)
      // alert(city)
      //this.$store.commit('MchangeCity', city)// 想要通过actions调用方法必须使用dispatch 或者 跳过actions直接通过commit来调用Mutations
      this.MchangeCity(city)
      this.$router.push('/') // 选择后跳转到主页 实现联动 用了vue-router的编程跳转链接
    },
    ...mapMutations(['MchangeCity'])//将changeCity映射为this.$store.commit('MchangeCity', city)
  }
}
</script>

<style lang='stylus' scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      background-color: #ccc
    &:after
      background-color: #ccc
  .border-bottom
      &:before
        background-color: #ccc
  .list
    position: absolute
    overflow: hidden
    left: 0
    right: 0
    top: 1.58rem
    bottom: 0
    .title
      line-height: .54rem
      background-color: #ccc
      padding-left: .2rem
      color: #666
      font-size: .26rem
    .button-list
      overflow: hidden
      padding: .1rem .2rem .1rem .1rem
      .button-wrapper
        float: left
        width: 33.33%
        .button
          margin: .1rem
          padding: .1rem
          text-align: center
          border: .02rem solid #ccc
          border-radius: .06rem
          color: #000
    .item-list
      line-height: .76rem
      padding-left: .2rem
      color: #666
</style>
